from http import HTTPStatus
from typing import Any

from aiohttp import ClientSession

from recruit_bot.api.base import AbstractRequests
from recruit_bot.api.exceptions import HTTPException


class Requests(AbstractRequests):
    async def post(self, url: str, json: dict[str, Any]) -> Any:
        async with ClientSession() as session:
            async with session.post(url, json=json) as response:
                self._check_status_code(response.status)
                if response.status != HTTPStatus.NO_CONTENT:
                    return None
                return await response.json()

    def _check_status_code(self, status_code: int) -> None:
        if not HTTPStatus(status_code).is_success:
            raise HTTPException()
